package com.hello.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hello.dao.Sdao;
import com.hello.domain.JsonTestUser;


@Controller
public class TestModelAndViewController {
	
	private Sdao dao;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}
	
	@Autowired
	public void setDao(Sdao dao) {
		this.dao = dao;
	}
	
	@RequestMapping(value = "/test/{testId}/testModelAndView.html", method = RequestMethod.GET)
	public ModelAndView showData(@PathVariable String testId) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("test");
		modelAndView.addObject("test", testId);
		modelAndView.addObject("name", AvailabilityStatus.notAvailable(testId, dao).getResults()[0]);
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/test/{testId}/{date}/testModelAndView.html", method = RequestMethod.GET)
	public ModelAndView showDateData(@PathVariable String testId, @PathVariable Date date) {
		ModelAndView model = new ModelAndView();
		model.setViewName("test");
		model.addObject("test", testId);
		model.addObject("name", date);
		return model;
	}
	
	@RequestMapping(value = "/test/{name}/testJSON.html", method = RequestMethod.GET)
	public @ResponseBody JsonTestUser getUser(@PathVariable String name) {
		JsonTestUser user = new JsonTestUser();
		user.setName(name);
		return user;
	}
}
